package annotool;

import ij.plugin.*;
import ij.gui.*;
import ij.*;
import ij.process.*;

import java.util.Collections;
import java.util.Vector;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import mpicbg.imagefeatures.Feature;


public class Visualization
{
	private static float scale = 1.0f;
	private static int fdsize = 4;
	public ImagePlus i;
	public float[] fs;

	public Visualization(ImagePlus i, float[] fs)
	{
		this.i = i;
		this.fs = fs;
	}
	/**
	 * draws a rotated square with center point  center, having size and orientation
	 */
	static void drawSquare( ImageProcessor ip, double[] o, double scale, double orient )
	{
		scale /= 2;
		
	    double sin = Math.sin( orient );
	    double cos = Math.cos( orient );
	    
	    int[] x = new int[ 6 ];
	    int[] y = new int[ 6 ];
	    

	    x[ 0 ] = ( int )( o[ 0 ] + ( sin - cos ) * scale );
	    y[ 0 ] = ( int )( o[ 1 ] - ( sin + cos ) * scale );
	    
	    x[ 1 ] = ( int )o[ 0 ];
	    y[ 1 ] = ( int )o[ 1 ];
	    
	    x[ 2 ] = ( int )( o[ 0 ] + ( sin + cos ) * scale );
	    y[ 2 ] = ( int )( o[ 1 ] + ( sin - cos ) * scale );
	    x[ 3 ] = ( int )( o[ 0 ] - ( sin - cos ) * scale );
	    y[ 3 ] = ( int )( o[ 1 ] + ( sin + cos ) * scale );
	    x[ 4 ] = ( int )( o[ 0 ] - ( sin + cos ) * scale );
	    y[ 4 ] = ( int )( o[ 1 ] - ( sin - cos ) * scale );
	    x[ 5 ] = x[ 0 ];
	    y[ 5 ] = y[ 0 ];
	    
	    ip.drawPolygon( new Polygon( x, y, x.length ) );
	}

	public void show( )
	{
		final ImagePlus imp = i;
		if ( imp == null )  { System.err.println( "There are no images open" ); return; }
		
		ImageProcessor ip2 = imp.getProcessor().duplicate().convertToRGB();
		
		ip2.setLineWidth( 1 );
		ip2.setColor( Color.blue );
		for ( int f = 0; f < fs.length / 132; f++ )
		{
			//System.out.println( f.location[ 0 ] + " " + f.location[ 1 ] + " " + f.scale + " " + f.orientation );
				drawSquare( ip2, new double[]{ fs[ (f * 132) + 2 ] / scale, fs[ (f * 132) + 3 ] / scale }, fdsize * 4.0 * ( double )fs[(f * 132) + 0] / scale, ( double )fs[(f * 132) + 1] );
		}
	
		ImagePlus imp1 = new ImagePlus( imp.getTitle() + " Features ", ip2 );
		imp1.show();
		BufferedImage buf = imp1.getBufferedImage();
	}
	
	public BufferedImage showSave(int width, Color color )
	{
		final ImagePlus imp = i;
		if ( imp == null )  { System.err.println( "There are no images open" ); return null; }
		
		ImageProcessor ip2 = imp.getProcessor().duplicate().convertToRGB();
		
		ip2.setLineWidth( width );
		ip2.setColor( color );
		for ( int f = 0; f < fs.length / 132; f++ )
		{
			//System.out.println( f.location[ 0 ] + " " + f.location[ 1 ] + " " + f.scale + " " + f.orientation );
				drawSquare( ip2, new double[]{ fs[ (f * 132) + 2 ] / scale, fs[ (f * 132) + 3 ] / scale }, fdsize * 4.0 * ( double )fs[(f * 132) + 0] / scale, ( double )fs[(f * 132) + 1] );
		}
	
		ImagePlus imp1 = new ImagePlus( imp.getTitle() + " Features ", ip2 );
		//imp1.show();
		BufferedImage buf = imp1.getBufferedImage();
		return buf;
	}
}